package com.audi.core.common;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

public class PageTag extends BodyTagSupport {

    @SuppressWarnings("rawtypes")
    private Page page;

    private String url;

    // private String styleClass;

    private String disableClass = "disabled";
    private String currentClass = "current";

    public int doEndTag() throws JspException {

        if (null == page) {
            return 0;
        }

        System.out.println("name:" + page.toString());
        try {
            HttpServletRequest request = (HttpServletRequest) super.pageContext.getRequest();

            /*
             * Object obj = request.getAttribute(name); if (obj == null) {
             * return 0; } if (!(obj instanceof Page)) { return 0; }
             */
            // Page page = (Page) obj;
            StringBuilder sb = new StringBuilder();

            // 可根据page的get方法设定内容,如下: &lt; Prev

            sb.append("<span class='" + (page.isHasPre() ? "" : disableClass) + "'> " + link(page, url, "pre")
                    + " </span>");

            for (int i = page.getStartPageIndex(); i < page.getEndPageIndex(); i++) {
                sb.append("<span class='" + (isCurrentPage(page, i) ? "currentClass" : "") + "'> " + link(page, url, i)
                        + " </span>");
            }

            sb.append("<span class='" + (page.isHasNext() ? "" : disableClass) + "'>" + link(page, url, "next")
                    + "</span>");

            JspWriter jspwriter = super.pageContext.getOut();
            jspwriter.write(sb.toString());
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 0;
    }

    private String link(Page page, String url, int i) {
        if (page.getPageNo() == i) {
            return i + "";
        } else {
            return "<a href='" + url + (isParameter(url) ? "&pageNo=" + i : "?pageNo=" + i) + "'>" + i + "</a>";
        }
    }

    private String link(Page page, String url, String tag) {

        if ("pre".equals(tag)) {
            if (page.isHasPre()) {
                return "<a href='"
                        + url
                        + (isParameter(url) ? "&pageNo=" + (page.getPageNo() - 1) : "?pageNo=" + (page.getPageNo() - 1))
                        + "'>&lt; Prev</a>";
            } else {
                return "&lt; Prev";
            }
        } else if ("next".equals(tag)) {
            if (page.isHasNext()) {
                return "<a href='"
                        + url
                        + (isParameter(url) ? "&pageNo=" + (page.getPageNo() + 1) : "?pageNo=" + (page.getPageNo() + 1))
                        + "'>Next &gt;</a>";
            } else {
                return "Next &gt;";
            }
        } else {

        }
        return null;
    }

    // url是否带有？
    private boolean isParameter(String url) {
        if (StringUtils.isBlank(url)) {
            return false;
        } else {
            return url.indexOf("?") > 0 ? true : false;
        }
    }

    // 是否为当前页
    private boolean isCurrentPage(Page page, Integer pageNo) {
        return page.getPageNo() == pageNo;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDisableClass() {
        return disableClass;
    }

    public void setDisableClass(String disableClass) {
        this.disableClass = disableClass;
    }

    public String getCurrentClass() {
        return currentClass;
    }

    public void setCurrentClass(String currentClass) {
        this.currentClass = currentClass;
    }

}
